package com.b2b.controller;

import com.b2b.model.OrdersDto;
import com.b2b.model.OrdersListDto;
import com.b2b.model.ServiceDto;
import com.b2b.model.common.ResponseBean;
import com.b2b.model.entity.Orders;
import com.b2b.model.entity.Service;
import com.b2b.model.entity.goods.Goods;
import com.b2b.service.CompanyInfoService;
import com.b2b.service.OrdersService;
import com.b2b.service.ServiceService;
import com.b2b.service.ShopService;
import com.b2b.service.goodsService.GoodsService;
import com.b2b.util.ServiceUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/service")
public class ServiceController {


    @Autowired
    ServiceService serviceService;

    @Autowired
    OrdersService ordersService;

    @Autowired
    CompanyInfoService companyInfoService;

    @Autowired
    ShopService shopService;

    @Autowired
    GoodsService goodsService;

    @Autowired
    ServiceUtil serviceDtoList;

    private final ServiceUtil serviceUtil;

    @Autowired
    public ServiceController(ServiceUtil serviceUtil){
        this.serviceUtil = serviceUtil;
    }

    @PostMapping("/add")
    public ResponseBean add(@RequestBody ServiceDto serviceDto){
        serviceService.insert(serviceDto);
        return new ResponseBean(HttpStatus.OK.value(), "新增成功(Insert Success)", null);
    }

    @DeleteMapping("/delete")
    public ResponseBean delete(@RequestParam Integer serviceId){
        serviceService.deleteByPrimaryKey(serviceId);
        return new ResponseBean(HttpStatus.OK.value(), "删除成功(Delete Success)", null);
    }

    @PutMapping("/handle")
    public ResponseBean handle(@RequestBody ServiceDto serviceDto){
        serviceService.updateByPrimaryKeySelective(serviceDto);
        return new ResponseBean(HttpStatus.OK.value(), "修改成功", null);
    }

    @PutMapping("/evaluate")
    public ResponseBean evaluate(@RequestBody ServiceDto serviceDto){
        serviceService.updateByPrimaryKeySelective(serviceDto);
        return new ResponseBean(HttpStatus.OK.value(), "修改成功", null);
    }

    @GetMapping("/getMySold")
    public ResponseBean getMySold(@RequestParam Map<String, Object> map){
        int page = 1, rows = 10;
        if(map.containsKey("page"))
            page = Integer.parseInt(map.get("page").toString());
        if(map.containsKey("rows"))
            rows = Integer.parseInt(map.get("rows").toString());

        Integer sellerId = Integer.parseInt(map.get("sellerId").toString());

        PageHelper.startPage(page, rows);
        PageHelper.orderBy("order_time" + " " + "desc");

        Orders orders = new Orders();
        orders.setSellerId(sellerId);
        List<Orders> ordersList = ordersService.select(orders);
        List<ServiceDto> serviceDtoList = new ArrayList<>();
        for (Orders o:ordersList
             ) {
            Service service = new Service();
            service.setOrderId(o.getOrderId());
            List<Service> tempList = serviceService.select(service);
            for (Service s:tempList
                 ) {
                Goods goods = goodsService.selectByPrimaryKey(s.getStockId());
                ServiceDto serviceDto = serviceUtil.getServiceDto(s);
                serviceDto.setBuyerId(o.getBuyerId());
                serviceDto.setBuyerName(companyInfoService.selectByPrimaryKey(o.getBuyerId()).getCompanyName());
                serviceDto.setGoodsId(goods.getGoodsId());
                serviceDto.setGoodsName(goods.getGoodsName());
                serviceDtoList.add(serviceDto);
            }
        }


        PageInfo<ServiceDto> selectPage = new PageInfo<>(serviceDtoList);
        Map<String, Object> result = new HashMap<>();
        result.put("count", selectPage.getTotal());
        result.put("serviceList", selectPage.getList());
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", result);
    }

    @GetMapping("/getMyBought")
    public ResponseBean getMyBought(@RequestParam Map<String, Object> map){
        int page = 1, rows = 10;
        if(map.containsKey("page"))
            page = Integer.parseInt(map.get("page").toString());
        if(map.containsKey("rows"))
            rows = Integer.parseInt(map.get("rows").toString());

        Integer buyerId = Integer.parseInt(map.get("buyerId").toString());

        PageHelper.startPage(page, rows);
        PageHelper.orderBy("order_time" + " " + "desc");

        Orders orders = new Orders();
        orders.setBuyerId(buyerId);
        List<Orders> ordersList = ordersService.select(orders);
        List<ServiceDto> serviceDtoList = new ArrayList<>();
        for (Orders o:ordersList
        ) {
            Service service = new Service();
            service.setOrderId(o.getOrderId());
            List<Service> tempList = serviceService.select(service);
            for (Service s:tempList
            ) {
                Goods goods = goodsService.selectByPrimaryKey(s.getStockId());

                ServiceDto serviceDto = serviceUtil.getServiceDto(s);
                serviceDto.setShopId(o.getShopId());
                serviceDto.setShopName(shopService.selectByPrimaryKey(o.getShopId()).getShopName());
                serviceDto.setGoodsId(goods.getGoodsId());
                serviceDto.setGoodsName(goods.getGoodsName());
                serviceDtoList.add(serviceDto);
            }
        }


        PageInfo<ServiceDto> selectPage = new PageInfo<>(serviceDtoList);
        Map<String, Object> result = new HashMap<>();
        result.put("count", selectPage.getTotal());
        result.put("serviceList", selectPage.getList());
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", result);
    }

}
